/**
 * login_auto.js
 * 用户重置密码（忘记密码）时，注册完成时希望能够自动登录
 * 
 * 自动登录步骤
 * 
 * 1：隐含登录页面基本输入框（username,password,execution,lt）
 * 2：业务功能成功时调用方法 user_login2();
 * @returns {Boolean}
 */

function user_login2() {
	//clearSSOCache();
	var gotoUrl = getParameter("url");
	if (gotoUrl == null || gotoUrl == "" || gotoUrl == undefined) {
		gotoUrl = "/" + i18n.name + "/index.html";
	}
		

	var form = $('#loginForm');
	form = form[0];
	var hostName=getCurHostName();
	var action=hostName+"/sso/login?loginView=json&service="+hostName+"/service/ticket/loginResult";
	$.ajax({
		url : action,
		dataType : 'json',
		async : false,
		success : function(data) {
			if (data.execution) {
				form.lt.value = data.lt;
				form.execution.value = data.execution;
						$.ajax({
							url : action,
							dataType : 'json',
							async : false,
							data : $('#loginForm').serialize(),
							success : function(data) {
								if (data.hasErr) {
									if (data.err == 'The credentials you provided cannot be determined to be authentic.'
										|| data.err =='用户名或者密码有误') {
										$("#login_Validform").text(
												i18n.login_psw_wrong).addClass(
												"Validform_wrong");

									} else if (data.err == 'Verifycode is a requird field'
										|| data.err=='验证码不能为空') {
										$('#hVeriryCode').val('1');
										$('#verifyCodeDiv').show();

									}else if(data.err=='The verify code you provided is error'
										|| data.err=='验证码有误'){
										$("#login_Validform").text(i18n.login_code_wrong).addClass("Validform_wrong");
										$('#hVeriryCode').val('1');
										$('#verifyCodeDiv').show();			
																					                	
									} else if(data.err=='disabled user' || data.err=='账号已停用'){
										$("#login_Validform").text(i18n.disabled_user).addClass("Validform_wrong");
									} else {
										$("#login_Validform").text(i18n.login_error).addClass("Validform_wrong");
									}
									
									$("#loginBtn").removeAttr("disabled");
								} else {
									syncCallService(
											"/service/ticket/loginResult",
											"get",
											"json",
											 '',
											function(data) {
												var t = data.t;
												var ct = data.ct;// 客户付费类型
												var uname=data.u;
												var loginStat=data.l;
												addCookieWithNoExpires("l", loginStat);
												addCookieWithNoExpires("t", t);
												addCookieWithNoExpires("ct", ct);
												addCookieWithNoExpires("u", uname);
												if(data.ehkmt=="1"){
													addCookieWithNoExpires("ehkmt", "1");
												}
												if(data.einter=="1"){
													addCookieWithNoExpires("einter", "1");
												}
												
												
												if("2" == ct){
			                                        addCookie("sub_permission", json2String(data.sub_permission));
			                                    }else if("1" == ct){
			                                        addCookieWithNoExpires("v", data.v);
			                                    }else{
			                                        delCookie("sub_permission");
			                                    }
												/*if(location.href.indexOf("/service/oauth")>-1){
													location.href = "/index.html";
													return;
												}*/
												
												if(orderValidateWindow != undefined){
													orderValidateWindow.close();
													var l = getL();
													
													if (l == 2 ) {
														$("#welcomeMsg").text(i18n.Welcome + getU());
														$("#_header_mem_register").hide();
														$("#_header_mem_login").hide();
														$("#mem_logout").show();
													}
													sfAlert('已登录成功');
												}
											}, function() {
												//alert("error");
											});
								}
							},error:function(data){
								$("#login_Validform").text("登录出错,请稍后重试").addClass("Validform_wrong");
							}
						});
			} else {
				//alert(1);
				//window.location.href = gotoUrl;
			}
		},error:function(data){
			$("#login_Validform").text("登录出错,请稍后重试").addClass("Validform_wrong");
		}
	});
	return false;

}

//点击刷新验证码
function changeimg() {
	var myimg = document.getElementById("imgCode");
	now = new Date();
	//myimg.src="/service/user/verifycode?xx="+now.getTime();
	var hostName=getCurHostName();
//	myimg.src = hostName+"/sso/verifyHandler?xx=" + now.getTime();
	myimg.src = hostName+"/sso/verifyHandler?xx=" + now.getTime();

}
$(document).ready(function(){
	var hostName=getCurHostName();
	$('#imgCode').attr('src',hostName+'/sso/verifyHandler');
});

